package com.qqqq.service.impl;

import com.qqqq.core.BusinessException;
import com.qqqq.core.Page;
import com.qqqq.dao.StudentDao;
import com.qqqq.domain.SignIn;
import com.qqqq.domain.Student;
import com.qqqq.service.StudentService;

import java.sql.SQLException;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao;

    public StudentServiceImpl(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public Student login(int stuNumber, String password) throws SQLException {
        Student target = this.studentDao.getStudentByNum(stuNumber);
        if (target == null) {
            throw new BusinessException(500, "学生不存在");
        }
        if (!target.getPassword().equals(password)) {
            throw new BusinessException(500, "您输入的密码有误");
        }
        return target;
    }

    @Override
    public int addStudentSign(int stuId) throws SQLException {
        return this.studentDao.addStudentSign(stuId);
    }

    @Override
    public Page<SignIn> pageSignIn(int stuId, int pageNum, int pageSize) throws SQLException {
        int total = this.studentDao.countSignIn(stuId);
        List<SignIn> signIns = this.studentDao.pageSignIn(stuId, pageNum, pageSize);
        return new Page<SignIn>(total, signIns);
    }

    @Override
    public int addStudent(Student student) throws SQLException {
        return this.studentDao.addStudent(student);
    }

    @Override
    public Student getStudent(int id) throws SQLException {
        return this.studentDao.getStudentById(id);
    }

    @Override
    public int updateStudent(Student student) throws SQLException {
        return this.studentDao.updateStudent(student);
    }

    @Override
    public int deleteStudent(int id) throws SQLException {
        return this.studentDao.deleteStudent(id);
    }

    @Override
    public Page<Student> pageStudent(Integer stuMajorId, Integer stuClassId, int pageNum, int pageSize) throws SQLException {
        int total = this.studentDao.countGetStudent(stuMajorId, stuClassId);
        List<Student> students = this.studentDao.pageGetStudent(stuMajorId, stuClassId, pageNum, pageSize);
        return new Page<Student>(total, students);
    }

}
